package cn.amaake.controller.student;

import com.jfinal.core.Controller;

import cn.amaake.common.Result;
import cn.amaake.common.model.Class;
import cn.amaake.common.model.Student;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;

public class StuWebController extends Controller {
	
	public void index() {
		Student student = getSessionAttr("stusession");
        set("username",student.getName());
		render("/views/stuweb/index/index.html");
	}
	
    public void home(){
    	Student student = getSessionAttr("stusession");
    	Class aclass = Class.dao.findById(student.getClassId());
    	set("classname", aclass.getClassName());
        set("username",student.getName());
        set("major", aclass.getMajor());
        set("teacher", aclass.getTeacher());
        render("/views/stuweb/home/index.html");
    }
    
    public void showupdatepassword(){
        render("/views/stuweb/index/updatepassword.html");
    }

    public void updatepassword(){
    	Student student = getSessionAttr("stusession");
        String password = getPara("password");
        Digester sha = new Digester(DigestAlgorithm.SHA256);
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHexpassword = sha.digestHex(md5.digestHex(password));
        student.setPassword(digestHexpassword);
        boolean boo = student.update();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("修改成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("修改失败！");
        }
        renderJson(result);
    }
    
}
